//
//  FloatingViewController.m
//  HollyPhoneSdk
//
//  Created by hollycrm on 2023/3/3.
//

#import "FloatingViewController.h"
#import "FloatingWindowView.h"
#import "HollyPhoneManager.h"

@interface FloatingViewController ()
{
    FloatingWindowView *floatView;
    UIPanGestureRecognizer *panGestureRecognizer;
    
}
@end

@implementation FloatingViewController


-(instancetype)init{
    self = [super init];
    if (self){
        panGestureRecognizer = [UIPanGestureRecognizer new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupViews];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callTimeChange) name:hollyPhoneCallTimeChange object:nil];
}

-(void)addGestrueRecongnizer:(UIWindow *)win{
    [panGestureRecognizer addTarget:self action:@selector(panGesture:)];
    [win addGestureRecognizer:panGestureRecognizer];
}

-(void)panGesture:(UIPanGestureRecognizer *) recognizer{
    CGPoint point = [recognizer translationInView:self.view.superview];
    UIView *view = recognizer.view;
    if (view == nil){
        return;
    }
    CGPoint center = CGPointMake(view.center.x + point.x, view.center.y + point.y);
    view.center = center;
    [recognizer setTranslation:CGPointZero inView:self.view.superview];
    if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled || recognizer.state == UIGestureRecognizerStateFailed){
        if (center.x < [UIScreen mainScreen].bounds.size.width/2){
            [self updateViewPosition:recognizer :true];
        }
        else{
            [self updateViewPosition:recognizer :false];
        }
        
    }
    
}

-(void)updateViewPosition:(UIPanGestureRecognizer *)recognizer :(Boolean)isLeft{
    [UIView animateWithDuration:0.3 animations:^{
        UIView *view = recognizer.view;
        if (view != nil){
            if(isLeft){
                view.frame = CGRectMake(0, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
            }
            else{
                view.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width - view.frame.size.width, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
            }
        }
        
    } completion:^(BOOL finished) {
        
    }];
}

-(void)callTimeChange{
    [floatView refreshFloatView];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)setupViews{
    floatView = [[FloatingWindowView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
    [self.view addSubview:floatView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
